//
//  AccountSecurityVC.swift
//  LeTu
//
//  Created by g1 on 2024/3/2.
//

import UIKit

class AccountSecurityVC: BaseViewController {

    private var viewModel = AccountSecurityVM()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        configUI()
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        getData()
    }
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        DispatchQueue.main.async {
            self.destoryAccountBtn.layerCornerRadius(self.destoryAccountBtn.height/2)
            self.logoutBtn.layerCornerRadius(self.logoutBtn.height/2)
        }
    }
    private func getData() {
        kUserManager.getUserInfomation { [weak self] dialog in
            guard let strongSelf = self else { return }
            DispatchQueue.main.async {
                strongSelf.viewModel.updateItems()
                strongSelf.infoTableView.reloadData()
            }
        }
    }
    
    // MARK: - UIButtonAction
    @objc func logoutBtnAction() {
        AppMiddleAlertView(title: "温馨提示".localized, content: "确认退出登录吗？".localized, leftTitle: "我再想想".localized, rightTitle: "确认退出".localized) {
            self.showIndicator()
            kUserManager.logout { [weak self] dialog in
                guard let strongSelf = self else { return }
                DispatchQueue.main.async {
                    strongSelf.hideIndicator()
                    if dialog.code == 200 {
                        strongSelf.navigationController?.popToRootViewController(animated: true)
                    } else {
                        strongSelf.showMessage(message: dialog.message)
                    }
                }
            }
        } cancelHolder: {}.show()

    }
    @objc func destoryAccountBtnAction() {
        
        AppMiddleAlertView(title: "温馨提示".localized, content: "账号注销后，将不能再登录本APP，用户数据将会被清除，确认注销账号吗？".localized, leftTitle: "取消".localized, rightTitle: "注销".localized) {
            self.showIndicator()
            self.viewModel.destoryAccount { [weak self] dialog in
                guard let strongSelf = self else { return }
                DispatchQueue.main.async {
                    strongSelf.hideIndicator()
                    if dialog.code == 200 {
                        kUserManager.logoutClearData()
                        strongSelf.backAction()
                    } else {
                        strongSelf.showMessage(message: dialog.message)
                    }
                }
            }
        } cancelHolder: { }.show()
        
    }
    
    // MARK: - configUI
    func configUI() {
        
        navIsHidden = false
        title = "账户安全".localized
        
        view.addSubview(infoTableView)
        view.addSubview(destoryAccountBtn)
        view.addSubview(logoutBtn)
        
        infoTableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        destoryAccountBtn.snp.makeConstraints { make in
            make.height.equalTo(CGFloat.app_btnHeight)
            make.left.equalTo(42)
            make.right.equalTo(-42)
            make.bottom.equalTo(-Size.safeAreaBottomGap-(iPhoneX ? 50:79))
        }
        logoutBtn.snp.makeConstraints { make in
            make.height.left.right.equalTo(destoryAccountBtn)
            make.bottom.equalTo(destoryAccountBtn.snp.top).offset(-16)
        }
        
        infoTableView.delegate = self
        infoTableView.dataSource = self
        infoTableView.register(cellClass: BaseArrowTableViewCell.self)
        infoTableView.register(cellClass: UserInfomationAvatarTableViewCell.self)
        
        logoutBtn.addTarget(self, action: #selector(logoutBtnAction))
        destoryAccountBtn.addTarget(self, action: #selector(destoryAccountBtnAction))
        
    }
    private lazy var infoTableView: UITableView = {
        let infoTableView = UITableView()
        infoTableView.tableFooterView = UIView()
        infoTableView.showsVerticalScrollIndicator = false
        return infoTableView
    }()
    private lazy var logoutBtn: GradientButton = {
        let logoutBtn = GradientButton()
        logoutBtn.gradientColors = UIColor.app_themeGradientColors
        logoutBtn.setTitle("退出登录".localized)
        logoutBtn.setFont(kSetPingFangBlod(15))
        return logoutBtn
    }()
    private lazy var destoryAccountBtn: CustomClickableView = {
        let destoryAccountBtn = CustomClickableView()
        destoryAccountBtn.setTitleColor(UIColor(red: 0.308, green: 0.271, blue: 0.271, alpha: 1))
        destoryAccountBtn.backgroundColor = .app_btnDisableBackColor
        destoryAccountBtn.setFont(kSetPingFangBlod(15))
        destoryAccountBtn.setTitle("注销账号".localized)
        return destoryAccountBtn
    }()

}

extension AccountSecurityVC: UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 10
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let header = UIView(frame: CGRect(x: 0, y: 0, width: screenWidth, height: tableView.sectionFooterHeight))
        header.backgroundColor = .app_viewBackgroundColor
        return header
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 68
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return viewModel.items.count
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel.items[section].count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(BaseArrowTableViewCell.self, indexPath: indexPath)
        let item = viewModel.items[indexPath.section][indexPath.row]
        cell.setItem(item)
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let item = viewModel.items[indexPath.section][indexPath.row]
        if item.title == "设置密码".localized {
            show(SetPasswordVC(), sender: nil)
        }
        
    }
    
}
